View Buffers :: 자바네트워크I/O[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

자바네트워크I/O
[1]
등록일:2008-03-12 09:45:49 (0%)
작성자:
제목:View Buffers
//  :  c12:ViewBuffers.java
//  From  'Thinking  in  Java,  3rd  ed.'  (c)  Bruce  Eckel  2002
//  www.BruceEckel.com.  See  copyright  notice  in  CopyRight.txt.

import  java.nio.ByteBuffer;
import  java.nio.CharBuffer;
import  java.nio.DoubleBuffer;
import  java.nio.FloatBuffer;
import  java.nio.IntBuffer;
import  java.nio.LongBuffer;
import  java.nio.ShortBuffer;

public  class  ViewBuffers  {
    public  static  void  main(String[]  args)  {
        ByteBuffer  bb  =  ByteBuffer
                .wrap(new  byte[]  {  0,  0,  0,  0,  0,  0,  0,  'a'  });
        bb.rewind();
        System.out.println("Byte  Buffer");
        while  (bb.hasRemaining())
            System.out.println(bb.position()  +  "  ->  "  +  bb.get());
        CharBuffer  cb  =  ((ByteBuffer)  bb.rewind()).asCharBuffer();
        System.out.println("Char  Buffer");
        while  (cb.hasRemaining())
            System.out.println(cb.position()  +  "  ->  "  +  cb.get());
        FloatBuffer  fb  =  ((ByteBuffer)  bb.rewind()).asFloatBuffer();
        System.out.println("Float  Buffer");
        while  (fb.hasRemaining())
            System.out.println(fb.position()  +  "  ->  "  +  fb.get());
        IntBuffer  ib  =  ((ByteBuffer)  bb.rewind()).asIntBuffer();
        System.out.println("Int  Buffer");
        while  (ib.hasRemaining())
            System.out.println(ib.position()  +  "  ->  "  +  ib.get());
        LongBuffer  lb  =  ((ByteBuffer)  bb.rewind()).asLongBuffer();
        System.out.println("Long  Buffer");
        while  (lb.hasRemaining())
            System.out.println(lb.position()  +  "  ->  "  +  lb.get());
        ShortBuffer  sb  =  ((ByteBuffer)  bb.rewind()).asShortBuffer();
        System.out.println("Short  Buffer");
        while  (sb.hasRemaining())
            System.out.println(sb.position()  +  "  ->  "  +  sb.get());
        DoubleBuffer  db  =  ((ByteBuffer)  bb.rewind()).asDoubleBuffer();
        System.out.println("Double  Buffer");
        while  (db.hasRemaining())
            System.out.println(db.position()  +  "  ->  "  +  db.get());
    }
}  ///:~


----------------------------------------------------------------------------------------------

1>  Buffer  클래스의  메서드
ㅁ  public  final  int  capacity()  :  이  버퍼의  용량(전체크기)를  리턴한다.
ㅁ  public  final  int  position()  :  이  버퍼의  위치를  리턴.
ㅁ  public  final  Buffer  position(int  newPosition)  :  이  버퍼의  위치를  newPosition으로  설정한다.
ㅁ  public  final  int  limit()  ;  이  버퍼의  limit를  리턴.
ㅁ  public  final  Buffer  limit(int  newLimit)  :  이  버퍼의  limit를  newlimit으로  설정한다.
ㅁ  public  final  Buffer  mark()  :  이  버퍼의  현재  위치에  마크를  설정.
ㅁ  public  final  Buffer  reset()
:  버퍼의  위치를  이전에  마크  한  위치에  되돌린다.  그렇다고  마크의  값이  마크가  파기되거나,  변경되지  않는다.
==>  예외:  InvalidMarkException  -  마크가  설정되어  있지  않은  경우에  reset()메서드를  호출한  경우  발생
ㅁ  public  final  Buffer  clear()
:  이  버퍼의  위치(position)는  0으로  limit와  capacity값과  같게  설정한다.  마크값은  파기된다.  한  번  사용한  버퍼를  새롭게  다시  쓰기  위해  설정한다.  이  메소드는,  read  조작  (put)을  실행하기  전에  주로  호출한다.  단,  버퍼내의  데이터를  지우는  것은  아니다.  단지  position과  limit를  초기값으로  설정한다.
ㅁ  public  final  Buffer  flip()
:  이  버퍼의  위치(position)는  0으로  limit와  position값과  같게  설정한다.  마크값은  파기된다.  이  메서드는  read  조작  (put)뒤,  write()나  get()하기전에  이  메소드를  호출한다.
==>  예   buf.put(magic);  //  Prepend  header
in.read(buf);  //  Read  data  into  rest  of  buffer
buf.flip();  //  Flip  buffer
out.write(buf);  //  Write  header  +  data  to  channel
ㅁ  public  final  Buffer  rewind()
:  이  버퍼의  위치만  0  으로  설정되어  마크는  파기된다.  이는  이미  put한  것을  다시  get할때나  이미  put한  것을  취소할때  사용한다.
ㅁ  public  final  int  remaining()
:  현재  위치에서  limit까지  읽어들일  수  있는  데이터의  개수를  리턴한다.
ㅁ  public  final  boolean  hasRemaining()
:  현재  위치에서  limit까지  하나  이상의  차이가  나는지를  boolean형으로  리턴해  준다.  하나  이상의  차이가  나면  하나  이상의  데이터를  get()하거나  put()할  수  있기  때문에  BufferOverflowException  이나  BufferUnderflowException  을  피하기  위한  목적으로  사용된다.  이  버퍼내에  요소가  1  개  이상  존재하는  경우  true를  리턴.
ㅁ  public  abstract  boolean  isReadOnly()
:  현재  이  버퍼가  get()만  가능하며  put()은  금지되어있는지  알려준다.
[본문링크] View Buffers
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=2515
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.